<?php

/**
 * 运营商审核退换货
 */

namespace app\home\controller;
use think\Lang;
use think\Db;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class OrderDa extends BaseSeller {

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'home/lang/'.config('default_lang').'/sellerreturn.lang.php');

    }

    /**
     * 退货记录列表页
     *
     */
    public function index() {

        $order_model = model('service');
        $condition = array();

        $order_sn = input('param.order_sn');
        if ($order_sn) {
            $condition['ds_s_order.order_sn'] = $order_sn;
        }
        $order_state = input('param.order_state');
        if (in_array($order_state, array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))) {
            $condition['ds_s_order.order_state'] = $order_state;
        }

        $order_type = input('param.order_type');
        if (in_array($order_type, array('1', '2', '3', '4'))) {
            $condition['ds_s_order.order_type'] = $order_type;
        }

        $invite_store_name = input('param.invite_store_name');
        if ($invite_store_name) {
            $condition['m.invite_store_name'] = $invite_store_name;
        }

        $buyer_name = input('param.buyer_name');
        if ($buyer_name) {
            $condition['ds_s_order.member_name'] = $buyer_name;
        }
        $query_start_time = input('param.query_start_time');
        $query_end_time = input('param.query_end_time');
        $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_start_time);
        $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $query_end_time);
        $start_unixtime = $if_start_time ? strtotime($query_start_time) : null;
        $end_unixtime = $if_end_time ? strtotime($query_end_time) : null;
        if ($start_unixtime || $end_unixtime) {
            $condition['ds_s_order.add_time'] = array('between', array($start_unixtime, $end_unixtime));
        }
        $condition['ds_s_order.da_member_id'] = $_SESSION['home']['member_id'];
        $order_list = $order_model->getapp_OrderList($condition, 10);

        $this->assign('return_list', $order_list);
        //分页
        $this->assign('show_page', $order_model->page_info->render());
//        $this->assign('show_page', $show_page);
        $this->assign('order_state', ['-1' => '已取消', '0' => '已作废', '1' => '待审核', '2' => '待支付', '3' => '待派单', '4' => '已完成', '5' => '已退款', '6' => '审核失败', '7' => '进行中', '8' => '已派单', '9' => '派单中', '10' => '大客户已审核', '11' => '大客户待审核', '12' => '大客户审核失败']);
        $this->assign('order_type', ['1' => '安装', '2' => '维修', '3' => '移机', '4' => '保养']);

        /* 设置卖家当前菜单  */
        $this->setSellerCurMenu('order');
        /* 设置卖家当前栏目 */
        $state = input('state');
        if (!in_array($state, array('deliverno', 'delivering', 'delivered'))) {
            $state = 'deliverno';
        }
        $this->setSellerCurItem('indexs');
//        var_dump($this->template_dir);exit;
        return $this->fetch($this->template_dir.'index');
    }

    public function edit()
    {
        $order_id = input('param.order_id');
        $state = input('param.state');
        $order = db('s_order')->where('order_id', '=', $order_id)->update(['order_state' => $state]);
        if ($order) {
            return json(['code' => 0 , 'message' => '操作成功']);
        }
        return json(['code' => -1 , 'message' => '操作失败']);
    }
    public function other()
    {
        $order_id = input('param.order_id');
        $state = input('param.other');
        $order = db('s_order')->where('order_id', '=', $order_id)->update(['order_reason' => $state]);
        if ($order) {
            return json(['code' => 0 , 'message' => '操作成功']);
        }
        return json(['code' => -1 , 'message' => '操作失败']);
    }

    public function show()
    {
        $order_id = intval(input('param.order_id'));
        if ($order_id <= 0) {
            $this->error(lang('miss_order_number'));
        }

        $order_model = model('service');
        $order_info = $order_model->getServiceOrderInfo($order_id);
        $order_mechanics = db('s_order_mechanic')
            ->alias('s')
            ->join('fuwu_personal p', 's.merid = p.id')
            ->field('s.*,p.name,p.phone')
            ->where('order_id', $order_id)
            ->order('s.id', 'desc')
            ->select();
        $appends = db('s_order_append')
            ->where('order_id', $order_id)
            ->select();
        foreach ($appends as $key => $value) {
            $appends[$key]['hours_total'] = $value['add_hours'] * $value['payper_hour'];
        }

        $app_order_info = db('order')->where(['order_id' => $order_info['order_mall_id']])->find();

        //订单变更日志
        //        $log_list = $order_model->getOrderlogList(array('order_id' => $order_info['order_id']));
        //        $this->assign('order_log', $log_list);
        $this->assign('appends', $appends);
        $this->assign('order_mechanics', $order_mechanics);
        $this->assign('order_state', ['-1' => '已取消', '0' => '已作废', '1' => '待审核', '2' => '待支付', '3' => '待派单', '4' => '已完成', '5' => '已退款', '6' => '审核失败', '7' => '进行中', '8' => '已派单', '9' => '派单中', '10' => '大客户已审核', '11' => '大客户待审核', '12' => '大客户审核失败']);;
        $this->assign('son_order_state', ['-1' => '已取消', '0' => '已作废', '1' => '待审核', '2' => '待支付', '3' => '支付完成', '4' => '已完成', '5' => '已退款', '6' => '审核失败', '7' => '进行中', '8' => '已接单', '9' => '派单中']);
        $this->assign('order_type', ['1' => '安装单', '2' => '维修单', '3' => '迁移机单', '4' => '保养单']);
        $this->assign('order_info', $order_info);

        $this->assign('app_order_mall_id', $app_order_info['apporder_id']);
        $this->assign('status', ['0' => '未接单', '1' => '已接单', '2' => '已拒单', '3' => '已完成']);
        $this->assign('appendStatus', ['0' => '未支付', '1' => '待支付', '2' => '已支付', '3' => '已取消', '4' => '已作废']);

        /* 设置卖家当前菜单  */
        $this->setSellerCurMenu('order');
        /* 设置卖家当前栏目 */
        $state = input('state');
        if (!in_array($state, array('deliverno', 'delivering', 'delivered'))) {
            $state = 'deliverno';
        }
        $this->setSellerCurItem('indexs');

        return $this->fetch($this->template_dir.'show');
    }


    /**
     * 用户中心右边，小导航
     *
     * @param string $menu_type 导航类型
     * @param string $menu_key 当前导航的menu_key
     * @return
     */
    protected function getMemberItemList() {
                 $menu_array = array(
                    array(
                        'name' => 'indexs', 'text' => '订单列表',
                        'url' => url('OrderDa/index')
                    )
                );

                return $menu_array;
    }



}
